package zhoug.common.clip;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultCaller;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import static android.app.Activity.RESULT_OK;

/**
 * @Author: zhoug
 * @Date: 2025-02-19
 * @Description:
 */
public abstract class BaseClipImageHelper implements IClipImageHelper {
    protected Context context;
    protected Uri inputData;
    protected Uri outputData;
    protected Bitmap.CompressFormat outputFormat= Bitmap.CompressFormat.JPEG;
    protected OnImageClipResultListener onImageClipResultListener;
    protected ActivityResultLauncher<Intent> mLauncher;


    protected BaseClipImageHelper(ComponentActivity activity) {
        this.context = activity;
        register(activity.getLifecycle(), activity);
    }

    protected BaseClipImageHelper(Fragment fragment) {
        this.context = fragment.getContext();
        register(fragment.getLifecycle(), fragment);
    }

    /**
     * 注册回掉
     * @param lifecycle
     * @param activityResultCaller
     */
    private void register(Lifecycle lifecycle, ActivityResultCaller activityResultCaller) {
        lifecycle.addObserver(new DefaultLifecycleObserver() {
            @Override
            public void onCreate(@NonNull LifecycleOwner owner) {
                mLauncher = activityResultCaller.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult activityResult) {
                        boolean success;
                        if (activityResult != null) {
                            success = activityResult.getResultCode() == RESULT_OK;
                        } else {
                            success = false;
                        }
                        if (onImageClipResultListener != null) {
                            onImageClipResultListener.onImageClipResult(success);
                        }
                    }
                });
            }

            @Override
            public void onDestroy(@NonNull LifecycleOwner owner) {
                if (mLauncher != null) {
                    mLauncher.unregister();
                    mLauncher = null;
                }
            }
        });
    }

    @Override
    public BaseClipImageHelper setContext(Context context) {
        this.context = context;
        return this;
    }

    /**
     * 设置需要裁剪的图片
     *
     * @param inputData
     * @return
     */
    @Override
    public BaseClipImageHelper setInputData(Uri inputData) {
        this.inputData = inputData;
        return this;
    }

    /**
     * 设置裁剪后输出的图片
     *
     * @param outputData
     * @return
     */
    @Override
    public BaseClipImageHelper setOutputData(Uri outputData) {
        this.outputData = outputData;
        return this;
    }

    /**
     * 设置裁剪后输出的图片 的格式
     *
     * @param outputFormat
     * @return
     */
    @Override
    public BaseClipImageHelper setOutputFormat(Bitmap.CompressFormat outputFormat) {
        this.outputFormat = outputFormat;
        return this;
    }

    /**
     * 设置裁剪监听
     *
     * @param onImageClipResultListener
     * @return
     */
    @Override
    public BaseClipImageHelper setOnImageClipResultListener(OnImageClipResultListener onImageClipResultListener) {
        this.onImageClipResultListener = onImageClipResultListener;
        return this;
    }


}
